package com.aiyige.utils;

import android.text.TextUtils;

import com.aiyige.model.moment.entity.Moment;
import com.aiyige.page.publish.util.PublishUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.RejectedExecutionException;

/**
 * Created by zhuyakun on 2018/1/19.
 */

public class MomentUtil {
    public static int convertAiyigeApplicationToPublishSource(String value) {
//        if (TextUtils.isEmpty(value))
//            return PublishUtil.PUBLISH_SOURCE_UNKNOWN;
//        switch (value) {
//            case Moment.MOMENT_AIYIGE_APPLICATION_ANDROID:
//                return PublishUtil.PUBLISH_SOURCE_ANDROID;
//            case Moment.MOMENT_AIYIGE_APPLICATION_PC:
//                return PublishUtil.PUBLISH_SOURCE_WEB_BROWSER;
//            case Moment.MOMENT_AIYIGE_APPLICATION_SOURCE_IOS:
//                return PublishUtil.PUBLISH_SOURCE_IOS;
//            default:
//                return PublishUtil.PUBLISH_SOURCE_UNKNOWN;
//        }
        return PublishUtil.PUBLISH_SOURCE_ANDROID;
    }

    public static int convertSubjectToSubjectIndex(String subject) {
        if (TextUtils.isEmpty(subject)) {
            return 0;
        }
        switch (subject) {
            case Moment.SUBJECT_PHOTO: {
                return Moment.SUBJECT_PHOTO_INDEX;
            }
            case Moment.SUBJECT_VIDEO: {
                return Moment.SUBJECT_VIDEO_INDEX;
            }
            case Moment.SUBJECT_ARTICLE: {
                return Moment.SUBJECT_ARTICLE_INDEX;
            }
            case Moment.SUBJECT_SPECIAL_TOPIC: {
                return Moment.SUBJECT_SPECIAL_TOPIC_INDEX;
            }
            case Moment.SUBJECT_VIDEO_COURSE: {
                return Moment.SUBJECT_VIDEO_COURSE_INDEX;
            }
            case Moment.SUBJECT_MAJOR_COURSE: {
                return Moment.SUBJECT_MAJOR_COURSE_INDEX;
            }
            case Moment.SUBJECT_TRAINING_COURSE: {
                return Moment.SUBJECT_TRAINING_COURSE_INDEX;
            }
            case Moment.SUBJECT_TRAINING_CARD: {
                return Moment.SUBJECT_TRAINING_CARD_INDEX;
            }
            case Moment.SUBJECT_PRIVATE_COURSE: {
                return Moment.SUBJECT_PRIVATE_COURSE_INDEX;
            }
            default: {
                return Moment.SUBJECT_UNKNOWN_INDEX;
            }
        }
    }

    public static void batchPopulateSubjectIndex(List<Moment> momentList) {
        for (Moment moment : momentList) {
            moment.setSubjectIndex(convertSubjectToSubjectIndex(moment.getSubject()));
        }
    }

    public static String extractRejectReason(String rejectJson) {
        if (TextUtils.isEmpty(rejectJson)) return "";
        try {
            StringBuilder stringBuilder = new StringBuilder();
            JSONArray jsonArray = JSON.parseArray(rejectJson);
            int upLimit = jsonArray.size();
            for (int i = 0; i < upLimit; ++i) {
                try {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String content = jsonObject.getString("content");
                    if (TextUtils.isEmpty(content)) continue;
                    stringBuilder.append(content).append("\n");
                } catch (Exception e) {

                }
            }
            return stringBuilder.toString();
        } catch (Exception e) {
            return "";
        }
    }
}
